<template>
	<fui-tabs :tabs="tabs" @change="changeTab" :short="false" selectedColor="#b88f5e" sliderBackground="#b88f5e" />
	<view v-if="currentTab === '待送达'">
		<view style="margin-top: 20px;" v-if="messageOne.length ===  0">
			<fui-empty src="https://zjdemo.xuande.work:8889/photo/65cfff1d2fbe4ceeac42524d021f21ff.png" title="暂无订单记录">
				<fui-button text="返回首页" :size="28" width="336rpx" height="84rpx" radius="100rpx" background="#fff"
					:margin="['64rpx','0']" borderColor="#333" color="#333" @click="goIndex"></fui-button>
			</fui-empty>
		</view>
		<view v-else>
			<view class="container" v-for="(food,index) in foodOrder">
				<view class="order-box">
					<view class="header">
						<view class="store-name">订单编号：{{food.orderId}}</view>
						<view class="status">{{orderType}}</view>
					</view>
					<view class="item" v-for="(items,itemsIndex) in food.cao" @click="godetailsOrder(food,items)">
						<image class="item-image" :src="items.comboImg"></image>
						<view class="item-details">
							<view class="item-title">{{items.comboName}}</view>
							<view class="item-options-quantity">
								<view class="item-options">{{items.comboContain}}</view>
								<view class="item-quantity">x{{nums[index][itemsIndex]}}</view>
							</view>
						</view>
					</view>

					<view class="footer">
						<view class="timestamp">{{food.createTime}}</view>
						<view class="total">共{{totalNums[index]}}件商品 合计: ¥{{food.money}}</view>
					</view>
					<view class="button-container">
						<button class="reorder-button" @click="godetailsOrder(food,items)">待送达</button>
					</view>
				</view>
			</view>
		</view>


	</view>
	<view v-else-if="currentTab === '已送达'">
		<view style="margin-top: 20px;" v-if="messageThree === '您还没有已送达的订单'">
			<fui-empty src="https://zjdemo.xuande.work:8889/photo/65cfff1d2fbe4ceeac42524d021f21ff.png" title="暂无订单记录">
				<fui-button text="返回首页" :size="28" width="336rpx" height="84rpx" radius="100rpx" background="#fff"
					:margin="['64rpx','0']" borderColor="#333" color="#333" @click="goIndex"></fui-button>
			</fui-empty>
		</view>
		<view v-else>
			<view class="container" v-for="(food,index) in foodOrders">
				<view class="order-box">
					<view class="header">
						<view class="store-name">订单编号：{{food.orderId}}</view>
						<view class="status">{{orderType}}</view>
					</view>
					<view class="item" v-for="(items,itemsIndex) in food.cao" @click="godetailsOrders(food,items)">
						<image class="item-image" :src="items.comboImg"></image>
						<view class="item-details">
							<view class="item-title">{{items.comboName}}</view>
							<view class="item-options-quantity">
								<view class="item-options">{{items.comboContain}}</view>
								<view class="item-quantity">x{{numss[index][itemsIndex]}}</view>
							</view>
						</view>
					</view>

					<view class="footer">
						<view class="timestamp">{{food.createTime}}</view>
						<view class="total">共{{totalNumss[index]}}件商品 合计: ¥{{food.money}}</view>
					</view>
					<view class="button-container">
						<button class="reorder-button" @click="godetailsOrders(food,items)">已送达</button>
					</view>
				</view>
			</view>
		</view>
	</view>

	<view v-else-if="currentTab === '全部订单'">
		<view style="margin-top: 20px;" v-if="messageTwo.length === 0">
			<fui-empty src="https://zjdemo.xuande.work:8889/photo/65cfff1d2fbe4ceeac42524d021f21ff.png" title="暂无订单记录">
				<fui-button text="返回首页" :size="28" width="336rpx" height="84rpx" radius="100rpx" background="#fff"
					:margin="['64rpx','0']" borderColor="#333" color="#333" @click="goIndex"></fui-button>
			</fui-empty>
		</view>
		<view v-else>
			<view class="container" v-for="(food,index) in foodOrderTwo">
				<view class="order-box">
					<view class="header">
						<view class="store-name">订单编号：{{food.orderId}}</view>
						<view class="status">{{orderType}}</view>
					</view>
					<view class="item" v-for="(items,itemsIndex) in food.cao" @click="godetailsTwo(food,items)">
						<image class="item-image" :src="items.comboImg"></image>
						<view class="item-details">
							<view class="item-title">{{items.comboName}}</view>
							<view class="item-options-quantity">
								<view class="item-options">{{items.comboContain}}</view>
								<view class="item-quantity">x{{numTwo[index][itemsIndex]}}</view>
							</view>
						</view>
					</view>

					<view class="footer">
						<view class="timestamp">{{food.createTime}}</view>
						<view class="total">共{{totalNumTwo[index]}}件商品 合计: ¥{{food.money}}</view>
					</view>
					<view class="button-container">
						<button class="reorder-button" v-if="isGet[index]==='取消'"
							@click="godetailsTwo(food,items)">已取消</button>
						<button class="reorder-button" @click="godetailsTwo(food,items)"
							v-else>{{isGet[index]}}</button>
					</view>
				</view>
			</view>
		</view>
	</view>


	<view v-else-if="currentTab === '已退款'">
		<view style="margin-top: 20px;" v-if="messageFour.length === 0">
			<fui-empty src="https://zjdemo.xuande.work:8889/photo/65cfff1d2fbe4ceeac42524d021f21ff.png" title="暂无订单记录">
				<fui-button text="返回首页" :size="28" width="336rpx" height="84rpx" radius="100rpx" background="#fff"
					:margin="['64rpx','0']" borderColor="#333" color="#333" @click="goIndex"></fui-button>
			</fui-empty>
		</view>
		<view v-else>
			<view class="container" v-for="(food,index) in foodOrderOne">
				<view class="order-box">
					<view class="header">
						<view class="store-name">订单编号：{{food.orderId}}</view>
						<view class="status">{{orderType}}</view>
					</view>
					<view class="item" v-for="(items,itemsIndex) in food.cao" @click="godetailsOrderOne(food,items)">
						<image class="item-image" :src="items.comboImg"></image>
						<view class="item-details">
							<view class="item-title">{{items.comboName}}</view>
							<view class="item-options-quantity">
								<view class="item-options">{{items.comboContain}}</view>
								<view class="item-quantity">x{{numOne[index][itemsIndex]}}</view>
							</view>
						</view>
					</view>

					<view class="footer">
						<view class="timestamp">{{food.createTime}}</view>
						<view class="total">共{{totalNumOne[index]}}件商品 合计: ¥{{food.money}}</view>
					</view>
					<view class="footer" v-if="food.riderRemark===''">
						<view class="timestamp">退款原因：无</view>
					</view>
					<view v-else class="footer">
						<view class="timestamp">退款原因：{{food.riderRemark}}</view>
					</view>
					<view class="button-container">
						<button class="reorder-button" @click="godetailsOrderOne(food,items)">已退款</button>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	const tabs = ref(['全部订单', '待送达', '已送达', '已退款']);
	const currentTab = ref('全部订单');


	import {
		url
	} from '@/utils/request.js';
	import {
		ref,
		onMounted
	} from 'vue';
	import {
		onShow,
		onLoad
	} from '@dcloudio/uni-app';
	import {
		onPullDownRefresh,
		onReachBottom
	} from "@dcloudio/uni-app";
	const orderType = ref('订单')
	const nums = ref([]);
	const numOne = ref([]);
	const numss = ref([]);
	const numTwo = ref([]);
	//todossss是退款订单
	const numssss = ref([]);
	const foodOrderTwo = ref([]);
	const foodOrder = ref([]);
	const foodOrders = ref([]);
	const foodOrderOne = ref([]);
	const foodOrdersss = ref([]);
	const totalNums = ref([]);
	const totalNumOne = ref([]);
	const totalNumss = ref([]);
	const totalNumTwo = ref([]);
	const totalNumssss = ref([]);
	const messageOne = ref('')
	const messageTwo = ref('')
	const messageThree = ref(null)
	const messageFour = ref('')
	const isGet = ref([])
	const fetchData = () => {
		uni.getStorage({
			key: 'openId',
			success: function(res) {
				uni.request({
					url: url + '/placeAnOrder/userSelectMyNoGetOrder',
					method: 'GET',
					data: {
						openId: res.data
					},
					success: (res) => {
						if (res.data && res.data.data) {
							foodOrder.value = res.data.data;
							let allNums = [];
							let allTotalNums = [];

							res.data.data.forEach(order => {
								let numArray = order.num.split(',').map(Number);
								allNums.push(numArray);
								let total = numArray.reduce((a, b) => a + b, 0);
								allTotalNums.push(total);
							});
							messageOne.value = res.data.data
							nums.value = allNums;
							totalNums.value = allTotalNums;

						} else {
							console.log("No data found for userSelectMyNoGetOrder");
						}
					}
				});
				uni.request({
					url: url + '/placeAnOrder/userSelectMyAllOrder',
					method: 'GET',
					data: {
						openId: res.data
					},
					success: (res) => {
						if (res.data && res.data.data) {
							foodOrderTwo.value = res.data.data;
							let allNumTwo = [];
							let allTotalNumTwo = [];
							let isGetTwo = []
							isGet.value = res.data.data.isGet
							console.log(isGet)
							res.data.data.forEach(order => {
								let numArrayTwo = order.num.split(',').map(Number);
								allNumTwo.push(numArrayTwo);
								let totalTwo = numArrayTwo.reduce((a, b) => a + b, 0);
								allTotalNumTwo.push(totalTwo);
								let allIsGet = order.isGet
								isGetTwo.push(allIsGet)
							});
							messageTwo.value = res.data.data
							isGet.value = isGetTwo
							numTwo.value = allNumTwo;
							totalNumTwo.value = allTotalNumTwo;
							console.log(isGet)
						} else {
							console.log("No data found for userSelectMyNoGetOrder");
						}
					}
				});
				uni.request({
					url: url + '/placeAnOrder/userSelectMyOKOrder',
					method: 'GET',
					data: {
						openId: res.data
					},
					success: (res) => {
						messageThree.value = res.data.message

						if (res.data && res.data.data) {
							foodOrders.value = res.data.data;
							let allNumss = [];
							let allTotalNumss = [];

							res.data.data.forEach(order => {
								let numArrays = order.num.split(',').map(Number);
								allNumss.push(numArrays);
								let totals = numArrays.reduce((a, b) => a + b, 0);
								allTotalNumss.push(totals);
							});
							numss.value = allNumss;
							totalNumss.value = allTotalNumss;

						} else {
							console.log(res.data);
							console.log("No data found for userSelectMyOKOrder");
						}
					}
				});
				uni.request({
					url: url + '/placeAnOrder/userSelectUnGetOrder',
					method: 'GET',
					data: {
						openId: res.data
					},
					success: (res) => {
						messageFour.value = res.data.data
						if (messageFour.value.leng)
							console.log(messageFour.value, "你好")
						if (res.data && res.data.data) {
							foodOrderOne.value = res.data.data;
							let allNumOne = [];
							let allTotalNumOne = [];

							res.data.data.forEach(order => {
								let numArrayOne = order.num.split(',').map(Number);
								allNumOne.push(numArrayOne);
								let totalOne = numArrayOne.reduce((a, b) => a + b, 0);
								allTotalNumOne.push(totalOne);
							});
							numOne.value = allNumOne;
							totalNumOne.value = allTotalNumOne;

						} else {
							console.log(res.data);
							console.log("No data found for userSelectMyOKOrder");
						}
					}
				});
			}
		});
	};

	onLoad((update) => {
		fetchData();
		console.log(update.yituikuan, "nihao")
	});

	onPullDownRefresh(() => {
		console.log("下拉刷新");
		fetchData();
		setTimeout(() => {
			console.log('End pulldown refresh');
			uni.stopPullDownRefresh();
		}, 1000);
	});

	onReachBottom(() => {
		console.log("上拉加载更多");
		fetchData();
		setTimeout(() => {
			console.log('End reach bottom refresh');
		}, 1000);
	});

	const changeTab = (tab) => {
		currentTab.value = tabs.value[tab.index];
	};

	const goIndex = () => {
		uni.switchTab({
			url: '../index/index'
		});
	};
	const godetailsOrder = (food, items) => {
		console.log(2222);
		const index = foodOrder.value.findIndex(order => order.orderId === food.orderId);
		if (index !== -1) {
			const orderDetails = {
				orderId: food.orderId,
				createTime: food.createTime,
				money: food.money,
				items: food.cao,
				nums: nums.value[index],
				totalNums: totalNums[index]
			};
			const orderDetailsStr = encodeURIComponent(JSON.stringify(orderDetails));
			uni.navigateTo({
				url: `/pages/detailsOrder/detailsOrder?orderDetails=${orderDetailsStr}`
			});
		}
	};

	const godetailsTwo = (food, items) => {
		console.log(2222);
		const index = foodOrderTwo.value.findIndex(order => order.orderId === food.orderId);
		if (index !== -1) {
			const orderDetails = {
				orderId: food.orderId,
				createTime: food.createTime,
				money: food.money,
				items: food.cao,
				nums: numTwo.value[index],
				totalNums: totalNumTwo[index]
			};
			const orderDetailsStr = encodeURIComponent(JSON.stringify(orderDetails));
			uni.navigateTo({
				url: `/pages/detailsOrder/detailsOrder?orderDetails=${orderDetailsStr}`
			});
		}
	};
	const godetailsOrders = (food, items) => {
		console.log(2222);
		const index = foodOrders.value.findIndex(order => order.orderId === food.orderId);
		if (index !== -1) {
			const orderDetails = {
				orderId: food.orderId,
				createTime: food.createTime,
				money: food.money,
				items: food.cao,
				nums: numss.value[index],
				totalNums: totalNumss.value[index]
			};
			const orderDetailsStr = encodeURIComponent(JSON.stringify(orderDetails));
			uni.navigateTo({
				url: `/pages/detailsOrder/detailsOrder?orderDetails=${orderDetailsStr}`
			});
		}
	};
	const godetailsOrderOne = (food, items) => {

		const index = foodOrderOne.value.findIndex(order => order.orderId === food.orderId);
		console.log(index);
		if (index !== -1) {
			const orderDetails = {
				orderId: food.orderId,
				createTime: food.createTime,
				money: food.money,
				items: food.cao,
				nums: numOne.value[index],
				totalNums: totalNumOne.value[index]
			};
			const orderDetailsStr = encodeURIComponent(JSON.stringify(orderDetails));
			uni.navigateTo({
				url: `/pages/detailsOrder/detailsOrder?orderDetails=${orderDetailsStr}`
			});
		}
	};
</script>

<style>
	.container {
		padding: 10px;
	}

	.order-box {
		border: 1px solid #ddd;
		border-radius: 10px;
		padding: 10px;
		background-color: #fff;
	}

	.header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 10px;
	}

	.status {
		color: #333;
		font-size: 14px;
	}

	.store-name {
		font-size: 14px;
		word-spacing: 10px;
	}

	.item {
		display: flex;
		border-bottom: 1px solid #f5f5f5;
		padding-bottom: 16px;
		margin-bottom: 16px;
	}

	.item-image {
		width: 36px;
		height: 36px;
		margin-right: 16px;
		align-self: center;
	}

	.item-details {
		flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: center;
	}

	.item-title {
		font-size: 16px;
		/* font-weight: bold; */
	}

	.item-options-quantity {
		display: flex;
		justify-content: space-between;
		align-items: center;
		font-size: 14px;
		color: #666;
		margin-bottom: 8px;
	}

	.item-quantity {
		color: #999;
	}

	.footer {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-top: 10px;
	}

	.timestamp {
		font-size: 14px;
		color: #666;
	}

	.total {
		font-size: 16px;
		font-weight: bold;
	}

	.button-container {
		display: flex;
		justify-content: flex-end;
		margin-top: 10px;
	}

	.reorder-button {
		background-color: #333;
		color: #fff;
		border: none;
		padding: 5px 20px;
		margin-right: 0px;
		border-radius: 26px;
		font-size: 14px;
	}
</style>